package com.boot.system.controller;

import cn.hutool.json.JSONUtil;
import com.boot.common.anno.AnnoLog;
import com.boot.common.anno.RepeatSubmit;
import com.boot.common.result.Result;
import com.boot.system.entity.SysDict;
import com.boot.system.service.DictService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequiredArgsConstructor
@RestController
@RequestMapping("/dict")
public class DictController {

    private final DictService dictService;

    @AnnoLog(value = "字典新增")
    @RepeatSubmit
    @PostMapping("/add")
    public Result<?> addDict(@RequestBody Map<String, Object> dataParam) {
        return dictService.addDict(dataParam);
    }

    @AnnoLog(value = "字典修改")
    @RepeatSubmit
    @PostMapping("/update")
    public Result<?> updateDict(@RequestBody Map<String, Object> dataParam) {
        return dictService.updateDict(dataParam);
    }

    @AnnoLog(value = "字典删除")
    @RepeatSubmit
    @PostMapping("/delete")
    public Result<?> deleteDict(@RequestBody Map<String, Object> dataParam) {
        return Result.success(dictService.deleteDict((String) dataParam.get("ids")));
    }

    @AnnoLog(value = "字典列表")
    @PostMapping("/list")
    public Result<?> dictList(@RequestBody Map<String, Object> queryParam) {
        return Result.success(dictService.getDictPage(queryParam));
    }

    @AnnoLog(value = "字典缓存列表")
    @GetMapping("/getList")
    public Result<?> getDictList(@RequestParam("code") String code) {
        SysDict sysDict = dictService.getCache(code);
        if (sysDict != null){
            return Result.success(JSONUtil.parseArray(sysDict.getItems()));
        }
        return Result.success();
    }
}
